// pages/order/order.js
import {
  getMyOrder
} from '../../api/order'
import {
  getUserInfo,
  getWechatFlag,
  pay
} from '../../api/public'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectFlag: -1,
    tabFlag:-1,
    orderList: [
      [],
      [],
      [],
      []
    ],
    pageNum: [
      1, 1, 1,1
    ],
    pageSize: 10,
    statusList: ['待付款', '已付款', '已完成'],
    userInfo: {},
    orderDetail: {},
    flag:undefined,
    payFlag: false,
    topNum:0,
    tableBottomNum:0,
    width:0
  },
  changeOrderSelect(e) {
    this.setData({
      selectFlag: parseInt(e.detail.current) - 1,
      orderList: this.data.orderList,
      topNum:0
    })
    if (this.data.orderList[parseInt(this.data.selectFlag) + 1].length == 0) {
      this.getOrderList()
    }
  },
  moveEnd(e) {
    this.setData({
      selectFlag: parseInt(e.detail.current) - 1,
      orderList: this.data.orderList,
      topNum:0,
      tabFlag:parseInt(e.detail.current) - 1
    })
    if (this.data.orderList[parseInt(this.data.selectFlag) + 1].length == 0) {
      this.getOrderList()
    }
  },
  fromData(e){
    var time =  new Date(e);
    return  `${(time.getMonth() + 1)}月${time.getDate()}日`
  },
  /**
   * 生命周期函数--监听页面加载
   */
  goOrderDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/orderDeatil/orderDetail?id=' + id,
    })
  },
  swiperMove(res){
     var left = (this.data.tabFlag + 1) * 25;
    this.setData({
      tableBottomNum:left + ((25 / this.data.width ) * res.detail.dx)
    })
  },
  settlement(res) {
    wx.showLoading({
      title: '加载中...',
    })
    this.data.orderId = res.currentTarget.dataset.data.orderNum;

    getUserInfo({}).then(e => {
      this.setData({
        userInfo: e.data.userInfo,
        payFlag: false,
      })
      if (this.data.userInfo.memberLevel == 0) {
        pay(1, this.data.orderId).then(e => {
          this.getOrderList()
        })
      } else {
        this.setData({
          payFlag: true,
          orderDetail: res.currentTarget.dataset.data
        })
        console.log(this.data.payFlag)
        wx.hideLoading()
      }
    })
  },
  selectOrder(e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      selectFlag: flag,
      orderList: this.data.orderList
    })
  },
  getOrderList() {
    wx.showNavigationBarLoading();
    this.data.pageNum[parseInt(this.data.selectFlag) + 1] = 1;
    getMyOrder({
      data: {
        status: this.data.selectFlag == -1 ? '':this.data.selectFlag,
        page: this.data.pageNum[parseInt(this.data.selectFlag) + 1],
        size: this.data.pageSize,
      }
    }).then(e => {
      console.log(e.data.data)
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      this.data.orderList[parseInt(this.data.selectFlag) + 1] = e.data;
      this.setData({
        orderList: this.data.orderList
      })
      console.log(this.data.orderList)
    })
  },
  scrolltolower(){
    wx.showNavigationBarLoading();
    this.data.pageNum[parseInt(this.data.selectFlag) + 1]++;
    getMyOrder({
      data: {
        status: this.data.selectFlag,
        page: this.data.pageNum[parseInt(this.data.selectFlag) + 1],
        size: this.data.pageSize
      }
    }).then(e => {
      wx.hideNavigationBarLoading();
      if (e.data.data.length == 0) return wx.showToast({
        title: '没有更多了',
        icon: "none"
      })
      e.data.data.forEach(e => {
        this.data.orderList[Number(this.data.selectFlag) + 1].push(e)
      })
      this.setData({
        orderList: this.data.orderList
      })

    })
  },
  onLoad: function (options) {
    var flag = options.flag ? options.flag : 0;
    this.setData({
      selectFlag: parseInt(flag)
    })
    wx.getSystemInfo({
      success: (result) => {
          this.data.width = result.screenWidth;
      },
    })
   
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getOrderList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onApply(e) {
    this.setData({
      selectFlag: 1,
      pageNum: 1
    })
    this.getOrderList();
  }
  /**
   * 用户点击右上角分享
   */
})